<template>
  <common-list class="dataTypeDiv">
    <!-- <div class="app-container"> -->
    <el-form
      slot="search"
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="标签名称" prop="tagName">
        <common-input
          v-model="queryParams.tagName"
          placeholder="请输入标签名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >查询
        </el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置 </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" slot="tools">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['system:tags:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:tags:remove']"
          >批量删除
        </el-button>
      </el-col>
    </el-row>

    <common-table
      slot="content"
      v-loading="loading"
      :data="tagsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="ID" align="left" prop="tagId" /> -->
      <el-table-column label="标签名称" align="left" prop="tagName" />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          {{
            scope.row.status === 0
              ? "启用"
              : scope.row.status === 1
              ? "停用"
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="left" prop="remark" />
      <el-table-column
        label="操作"
        align="left"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleUpdate(scope.row, 2)"
            v-hasPermi="['apisecurity:apiinterface:document']"
            >详情
          </el-button>
          <el-button
            type="text"
            @click="handleUpdate(scope.row, 1)"
            v-hasPermi="['system:tags:edit']"
            >编辑
          </el-button>
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:tags:remove']"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或编辑标签对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="标签名称" prop="tagName">
          <common-input v-model="form.tagName" placeholder="请输入标签名称" />
        </el-form-item>
        <el-form-item label="状态" v-if="form.tagId" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="0">启用</el-radio>
            <el-radio :label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <common-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm" :loading="saveLoading"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <el-dialog
      :title="detailsTitle"
      :visible.sync="detailsOpen"
      width="700px"
      custom-class="addOrEditClass"
      append-to-body
    >
      <el-form label-width="110px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="字段名称" prop="tagName">
              {{ form.tagName }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              {{ form.remark }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- </div> -->
  </common-list>
</template>

<script>
import {
  listTags,
  delTags,
  addTags,
  updateTags,
} from "@/api/datascan/tags/tags";

export default {
  name: "Tags",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示查询条件
      showSearch: true,
      // 总条数
      total: 0,
      // 标签表格数据
      tagsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      detailsOpen: false,
      detailsTitle: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tagName: null,
        status: null,
        createUserCode: null,
        updateUserCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tagName: [
          { required: true, message: "请输入标签名称", trigger: "blur" },
          {
            min: 2,
            max: 30,
            message: "长度在 2 到 30 个字符",
            trigger: "blur",
          },
        ],
        remark: [{ max: 140, message: "长度不能超过140", trigger: "blur" }],
      },
      saveLoading: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询标签列表 */
    getList() {
      this.loading = true;
      listTags(this.queryParams).then((response) => {
        if (response && (response.code === 200 || response.rescode === 200)) {
          this.tagsList = response.data.dataList;
          this.total = response.data.totalCount;
        }
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.detailsOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        tagId: undefined,
        tagName: undefined,
        status: undefined,
        createUserCode: undefined,
        createTime: undefined,
        updateUserCode: undefined,
        updateTime: undefined,
        remark: undefined,
        delFlag: undefined,
      };
      this.resetForm("form");
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.tagId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加标签";
    },
    /** 编辑按钮操作 */
    handleUpdate(row, n) {
      // 1:编辑  2详情
      this.reset();
      this.form = { ...row };
      if (n === 1) {
        this.open = true;
        this.title = "编辑标签";
      } else {
        this.detailsOpen = true;
        this.detailsTitle = "标签详情";
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.saveLoading = true;

      this.$refs["form"].validate((valid) => {
        const addParams = {
          createTime: undefined,
          updateTime: undefined,
          delFlag: undefined,
          checkFlag: undefined,
        };
        const newParams = Object.assign({}, this.form, addParams);
        if (valid) {
          if (this.form.tagId != null) {
            updateTags(newParams)
              .then((res) => {
                if (res && (res.code === 200 || res.rescode === 200)) {
                  this.$GlobalSuccessMessage("编辑成功");
                  this.queryParams.pageNum = 1;
                  this.getList();
                } else {
                  this.$GlobalErrorConfirm("编辑失败", res.msg);
                }
              })
              .finally(() => {
                this.open = false;
                this.$nextTick(() => {
                  setTimeout(() => {
                    this.saveLoading = false;
                  }, 1000);
                });
              });
          } else {
            addTags(newParams)
              .then((res) => {
                if (res && (res.code === 200 || res.rescode === 200)) {
                  this.$GlobalSuccessMessage("新增成功");
                  this.queryParams.pageNum = 1;
                  this.getList();
                } else {
                  this.$GlobalErrorConfirm("新增失败", res.msg);
                }
              })
              .finally(() => {
                this.open = false;
                this.$nextTick(() => {
                  setTimeout(() => {
                    this.saveLoading = false;
                  }, 1000);
                });
              });
          }
        } else {
          this.saveLoading = false;
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tagIds = row.tagId || this.ids;
      this.$GlobalDeleteConfirm()
        .then(() => {
          delTags({ tagId: row.tagId ? tagIds : tagIds.join(",") }).then(
            (res) => {
              if (res && (res.code === 200 || res.rescode === 200)) {
                this.queryParams.pageNum = 1;
                this.getList();
                this.$GlobalSuccessMessage("删除成功");
              } else {
                this.$GlobalErrorConfirm("删除失败", res.msg);
              }
            }
          );
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss">
.addOrEditClass {
  color: red;
  max-height: 66%;
  overflow-y: auto;
}
</style>
